import axios from 'axios'
import store from '@/store/index.js'

const request = axios.create({
  baseURL: 'http://api-toutiao-web.itheima.net'
})

// 请求拦截器
request.interceptors.request.use(config => {
  // 在发送请求之前做些什么
  const { user } = store.state
  if (user) {
    config.headers.Authorization = `Bearer ${user.token}`
  }
  return config
}, error => {
  // 对请求错误做些什么
  return Promise.reject(error)
})

export default request
